package com.excel.executors;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author User
 * @Description: 业务线程池
 */
public class BizExecutor {

    private static final int CORE_POOL_SIZE = 5;
    private static final int MAX_POOL_SIZE = 10;
    private static final long KEEP_ALIVE_TIME = 60L;
    private static final int BLOCK_QUEUE_SIZE = 1000;
    private static final String THREAD_NAME_FORMAT = "biz-%d";

    private BizExecutor() {
    }

    private static class Holder {
        static ThreadPoolExecutor instance = new ThreadPoolExecutor(
                CORE_POOL_SIZE,
                MAX_POOL_SIZE,
                KEEP_ALIVE_TIME,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(BLOCK_QUEUE_SIZE),
                new ThreadFactoryBuilder().setNameFormat(THREAD_NAME_FORMAT).build(),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }

    public static ThreadPoolExecutor getPool() {
        return Holder.instance;
    }

}
